import {observable, action} from 'mobx';
import * as goalMain from '../../api/goal/goalAdd'
import { SearchAdvanced, ListAdvanced, RouteLayout, Browser, BrowserHrm,Upload  } from 'weaver-mobile-page';
import { Tools ,SearchBar,Icon,Bubble} from 'weaver-mobile';
import * as Util from "../../util";
const getLabel = Tools.getLabel;
export default class GoalAddStore {
    
    @observable cateList = []; //目标的分类
    @observable loading = false; // 页面初始化的loading状态：数据加载成功前后前使用
    @observable params = {
        goalName: '',//目标名称
        enddate: '',//结束时间
        remark: '',//描述
        principalid: [],//责任人
        parentid: [],//上级目标
        partnerid: [],//参与人
        goalids:[],//相关目标
        taskids: [],//相关任务
        docids: [],//相关文档
        wfids: [],//相关流程
        crmids: [],//相关客户
        projectids: [],//相关项目
        relatedacc: "",//相关附件
        period:'',//周期
        cate:'',//分类  
    }

    @observable supGoalBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });//上级目标
    @observable supGoalBrowserValue = [];
    @observable principalHrmBrowser = new BrowserHrm.Store({ route: '/goalHome/goalAdd'}); //负责人
    @observable principalHrmBrowserValue = [];
    @observable partHrmBrowser = new BrowserHrm .Store({ route: '/goalHome/goalAdd' }); //参与人
    @observable partHrmBrowserValue = [];
    @observable realteGoalBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });//相关目标
    @observable realteGoalBrowserValue = [];
    @observable realteTaskBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });//相关任务
    @observable realteTaskBrowserValue = [];
    @observable realteDocBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });//相关文档
    @observable realteDocBrowserValue = [];
    @observable realteWorkFlowBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });//相关流程
    @observable realteWorkFlowBrowserValue = [];
    @observable realteCustomerBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });//相关客户
    @observable realteCustomerBrowserValue = [];
    @observable realteProBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });//相关项目
    @observable realteProBrowserValue = [];
    @observable uploadStore = new Upload.Store({ route: '/goalHome/goalAdd' });
    @observable relateUploadValue = [];
    // @observable browser2 = new Browser.Store({ route: '/browser/demo-3' });

    @action("获取目标的分类")
    getAllCates = (params) => {
        this.loading = true;
        goalMain.getAllCates(params).then(action(res => {
            this.loading = false;
            if(res.api_status){
                this.cateList = this.setCateList(res.cateList);
            }else{
                // message.error(res.msg || getLabel('25009','失败'));
                Bubble.error(getLabel('25009','失败'),1)
            }
        }));
    }

    @action
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            this[key] = params[key];
        });
    }
    

    /**
     * 设置选项
     */
    setCateList = (list)=>{
        let result = [];
        for(let i = 0;i < list.length;i ++){
            let option = {};
            option.key = list[i].id;
            option.showname = list[i].catename;
            result.push(option);
        }
        return result;
    }

    @action
    setValue = (value,type) =>{
        this.principalHrmBrowserValue = [];
        value.map((d,i)=>{
            if(type == "principal"){
                this.principalHrmBrowserValue.push({id:""+d.id,name:d.name});
            }
        })

    }

    @action
    setSupGoalBrowserValue = (id,name) =>{
        this.supGoalBrowserValue = [];
        this.supGoalBrowserValue.push({id,name})
    }

    @action
    setParamsValue = (params) => {
        this.params = params;
    }

    /**
     * 输入框的更改
     */
    @action
    onInputChange = (values, type) =>{
        if(type == 'period'){
            this.computeEndDate(values);
        }else{
            this.params[type] = values;
        }
    }

    /**
     * 更新浏览按钮的值
     */
    @action
    onChangeBrowser = (datas, type) => {
        this.params[type] = datas;
    }


    /**
     * 附件上传
     */
    @action
    onFileChange = (ids,files)=>{
        this.relateUploadValue = files;
        let relatedacc = ",";
        relatedacc += ids.toString()+","
        this.params["relatedacc"] = relatedacc;
    }

    /**
     * 计算结束日期
     */
    @action
    computeEndDate = (values,season,year) =>{
        let date = new Date()
        let y  = "";
        if(!Util.isEmpty(year)){
            y =  parseInt(year);
        }else{
            y = date.getFullYear();
        }
        let m = date.getMonth();
        if(values == "1"){
            this.params["enddate"]=Util.convertTimeToString(new Date(y,m+1,0));
        }else if(values == "2"){
            if(season == "1"){
                this.params["enddate"]=Util.convertTimeToString(new Date(y,3,0))
            }else if(season == "2"){
                this.params["enddate"]=Util.convertTimeToString(new Date(y,6,0))
            }else if(season == "2"){
                this.params["enddate"]=Util.convertTimeToString(new Date(y,9,0))
            }else{
                this.params["enddate"]=Util.convertTimeToString(new Date(y,12,0))
            }   
        }else if(values == "3"){
            this.params["enddate"]=Util.convertTimeToString(new Date(y+1,0,0))
        }else if(values == "4"){
            this.params["enddate"]=Util.convertTimeToString(new Date(y+4,0,0))
        }else if(values == "5"){
            this.params["enddate"]=Util.convertTimeToString(new Date(y+6,0,0))
        }
        this.params["period"]= values;
    }


    @observable
    category = ""
  
    @action
    changeCategory = (category) => {
      this.category = category
    }
  
    @observable
    maxUploadSize = ""
  
    @action
    changeMaxUploadSize = (maxUploadSize) => {
      this.maxUploadSize = maxUploadSize
    }

    @observable
    setLoading = (loading) =>{
        this.loading = loading;
    }

    @action
    clearData = () =>{
       this.params = {
            goalName: '',//目标名称
            enddate: '',//结束时间
            remark: '',//描述
            principalid: [],//责任人
            parentid: [],//上级目标
            partnerid: [],//参与人
            goalids:[],//相关目标
            taskids: [],//相关任务
            docids: [],//相关文档
            wfids: [],//相关流程
            crmids: [],//相关客户
            projectids: [],//相关项目
            relatedacc: "",//相关附件
            period:'',//周期
            cate:'',//分类 
        }
        // this.supGoalBrowser.clear();
        // this.partHrmBrowser.clear();
        this.realteGoalBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });
        this.realteTaskBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });
        this.realteDocBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });
        this.realteWorkFlowBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });
        this.realteCustomerBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });
        this.realteProBrowser = new Browser.Store({ route: '/goalHome/goalAdd' });
        // this.uploadStore.clear();

        this.supGoalBrowserValue = [];
        // this.principalHrmBrowserValue = [];
        this.partHrmBrowserValue = [];
        this.realteGoalBrowserValue = [];
        this.realteTaskBrowserValue = [];
        this.realteDocBrowserValue = [];
        this.realteWorkFlowBrowserValue = [];
        this.realteCustomerBrowserValue = [];
        this.realteProBrowserValue = [];
        this.relateUploadValue = [];
    }
}